package com.hspedu.newData.dmsxl.哈希表;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-11 
 * @Version: 1.0
 */

public class LC2099找到和最大的长度为K的子序列 {

    public int[] maxSubsequence(int[] nums, int k) {
        int len = nums.length;
        int[] res = new int[len];
        for (int i = 0; i < len; i++) {
            res[i] = nums[i];
        }
        Arrays.sort(res);
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = len - 1; i > len - k - 1; i--) {
            map.put(res[i],map.getOrDefault(res[i],0)+1);
        }

        int[] result = new int[k];
        int x = 0;
        for (int i = 0; i < len; i++) {
            if (x < k && map.containsKey(nums[i]) && map.get(nums[i]) > 0){
                result[x++] = nums[i];
                map.put(nums[i],map.get(nums[i]) - 1);
            }
        }
        return result;
    }

    @Test
    public void test() {
        // 测试代码

        maxSubsequence(new int[]{-1,-2,3,4},3);
    }

}
